/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.services;

import java.util.List;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.zoundry.gaedt.core.extensions.ZGaeExtensionLoader;
import org.zoundry.gaedt.services.applications.IZGaeApplicationsServiceObserver;
import org.zoundry.gaedt.services.applications.ZGaeApplicationsService;
import org.zoundry.gaedt.userinput.ZGaeUserInputService;


/**
 * The activator class controls the plug-in life cycle
 */
public class ZGaeServicesActivator extends AbstractUIPlugin
{
    // The plug-in ID
    public static final String PLUGIN_ID = "org.zoundry.gaedt.services";
    // The shared instance
    private static ZGaeServicesActivator sPlugin;

    /**
     * @return the plugin
     */
    private static ZGaeServicesActivator getPlugin()
    {
        return sPlugin;
    }

    /**
     * @param aPlugin the plugin to set
     */
    private static void setPlugin(ZGaeServicesActivator aPlugin)
    {
        sPlugin = aPlugin;
    }

    /**
     * The constructor
     */
    public ZGaeServicesActivator()
    {
    }

    /**
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext aContext) throws Exception
    {
        super.start(aContext);
        setPlugin(this);

        startServices();
        processExtensions();
    }

    /**
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext aContext) throws Exception
    {
        stopServices();

        setPlugin(null);
        super.stop(aContext);
    }

    /**
     * Starts all services.
     */
    protected void startServices()
    {
        ZGaeApplicationsService.getInstance().start();
        ZGaeUserInputService.getInstance().start();
    }

    /**
     * Reads all of the extensions contributed to us via our custom
     * extension points.
     */
    private void processExtensions()
    {
        List<IZGaeApplicationsServiceObserver> discoveredObservers = ZGaeExtensionLoader.discoverContributors(
                IZGaeApplicationsServiceObserver.EXTENSION_POINT_ID,
                IZGaeApplicationsServiceObserver.class);
        for (IZGaeApplicationsServiceObserver observer : discoveredObservers)
        {
            ZGaeApplicationsService.getInstance().addObserver(observer);
        }
    }

    /**
     * Stops all services.
     */
    protected void stopServices()
    {
        ZGaeApplicationsService.getInstance().stop();
        ZGaeUserInputService.getInstance().stop();
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static ZGaeServicesActivator getDefault()
    {
        return getPlugin();
    }
}
